package leetcode.day_2021_03_8_2021_03_14;

import com.sun.xml.internal.ws.util.StringUtils;

/**
 * 58. 最后一个单词的长度
 * https://leetcode-cn.com/problems/length-of-last-word/
 */
public class LeetCode0058 {

    public static int lengthOfLastWord(String s) {
        for (Character ch : s.toCharArray()) {
            if (!Character.isWhitespace(ch)) break;
        }

        int count = 0;
        for (int i=s.length()-1; i>=0; i--) {
            if (count == 0 && Character.isWhitespace(s.charAt(i))) continue;
            if (!Character.isWhitespace(s.charAt(i))) count++;
            else break;
        }

        return count;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLastWord("Hello World"));
        System.out.println(lengthOfLastWord(" "));
        System.out.println(lengthOfLastWord("a    "));
    }
}
